package android.support.v4.os;

import java.io.File;
import java.util.Map.Entry;

import android.content.ContentResolver;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.provider.Settings;
import android.provider.Settings.Secure;

import com.edroid.autorunner.um.UserManager;
import com.example.utils.KvTool;


/**
 * hook android.provider.Settings
 * 
 * @author yichou 2015-3-16
 *
 */
public class ESettings extends EHook {

	@Override
	public String getClassName() {
		return Settings.class.getName();
	}

	@Override
	public void hook() {
		new Settings_System().hook();
		new Settings_Secure().hook();
	}

	static final class Settings_System extends EHook {
		KvTool kv;
		
		public Settings_System() {
			kv = new KvTool(new File(EConstants.SETTINGS_SYSTEM));
		}
		
		@Override
		public String getClassName() {
			return Settings.System.class.getName();
		}
		
		@Override
		public void hook() {
			super.hook();
			
			//getString(ContentResolver resolver, String name)
			findAndHookMethod("getString", ContentResolver.class, String.class, this);
			//boolean putString(ContentResolver resolver, String name, String value)
			findAndHookMethod("putString", ContentResolver.class, String.class, String.class, this);
			
			//int getInt(ContentResolver cr, String name, int def) 
			findAndHookMethod("getInt", ContentResolver.class, String.class, this);
			findAndHookMethod("putInt", ContentResolver.class, String.class, int.class, this);
			
			findAndHookMethod("getLong", ContentResolver.class, String.class, this);
			findAndHookMethod("putLong", ContentResolver.class, String.class, long.class, this);
			
			findAndHookMethod("getFloat", ContentResolver.class, String.class, this);
			findAndHookMethod("putFloat", ContentResolver.class, String.class, float.class, this);
			
			//getConfiguration(ContentResolver cr, Configuration outConfig)
//			findAndHookMethod("getConfiguration", ContentResolver.class, Configuration.class, this);
			//boolean putConfiguration(ContentResolver cr, Configuration config)
//			findAndHookMethod("putConfiguration", ContentResolver.class, Configuration.class, this);
		}
		
		@Override
		protected boolean before(String name, Object[] args, MethodHookParam param) {
			if(name.startsWith("put")) {
				if(args.length > 2)
					log.i("Settings_System", name + " " + args[1] + " " + args[2]);
			}
				
				//记录保存下来的键值
				if(args[1] != null) {
					kv.put(args[1].toString(), args[1].getClass().getSimpleName()).commit();
				}
			
			return super.before(name, args, param);
		}
		
		@Override
		protected void after(String name, Object[] args, MethodHookParam param) {
			Object ret = param.getResult();
			
			if(name.startsWith("get")) {
				log.i("Settings_System", name + " " + args[1] + " = " + ret);
			}
		}
	}
	
	static final class Settings_Secure extends EHook {

		@Override
		public String getClassName() {
			return Secure.class.getName();
		}
		
		@Override
		public void hook() {
			log.d("hook Secure>>>");
			
			//String android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);
			findAndHookMethod("getString", ContentResolver.class, String.class, this);
			findAndHookMethod("getInt", ContentResolver.class, String.class, this);
			findAndHookMethod("getLong", ContentResolver.class, String.class, this);
		}

		@Override
		protected void after(String name, Object[] args, MethodHookParam param) {
			log.i("Settings_Secure", name + " " + args[1] + " = " + param.getResult());
			
			if(Secure.ANDROID_ID.equals(args[1])) {
				param.setResult(UserManager.currentUserInfo().aid);
			}
		}
	}
	
}

